/*
 * 机器输出测试
 * 舞者无罪
 * 2014.12.02
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "com.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "rock_tran.h"
#include "keypane.h"
#include <mh/common.h>
#include "rock_test.h"

#define OUTPUT_TEST_CAPTION_WIDTH   (140)
#define OUTPUT_TEST_CAPTION_HEIGHT  (24)
#define OUTPUT_TEST_CAPTION_INTER   (6)
#define OUTPUT_TEST_COL_WIDTH       (90) //total width / 3
#define OUTPUT_TEST_CAPTION_LEFT    (CX_EDGE_LEFT * 2)
#define OUTPUT_TEST_CAPTION_TOP     (CY_TITLE4_HEIGHT + CX_EDGE_LEFT - 5)
#define OUTPUT_TEST_NAME_WIDTH      (60)



/*
 *
 */
static void CreateRockTestInterface(HWND hWnd)
{
    CreateWindow (MH_LABEL_NAME,
        MW_ROCK_RUN_DIR,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        OUTPUT_TEST_CAPTION_LEFT,
        OUTPUT_TEST_CAPTION_TOP,
        OUTPUT_TEST_CAPTION_WIDTH,
        OUTPUT_TEST_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_ROCK_RUN_RESET,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        OUTPUT_TEST_CAPTION_LEFT,
        OUTPUT_TEST_CAPTION_TOP + (OUTPUT_TEST_CAPTION_HEIGHT + OUTPUT_TEST_CAPTION_INTER),
        OUTPUT_TEST_CAPTION_WIDTH,
        OUTPUT_TEST_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_ROCK_RUN_DIR,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        OUTPUT_TEST_CAPTION_LEFT,
        OUTPUT_TEST_CAPTION_TOP + (OUTPUT_TEST_CAPTION_HEIGHT + OUTPUT_TEST_CAPTION_INTER) * 2,
        OUTPUT_TEST_CAPTION_WIDTH,
        OUTPUT_TEST_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
        CreateWindow (MH_LABEL_NAME,
        MW_ROCK_RUN_NEEDLE,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        OUTPUT_TEST_CAPTION_LEFT,
        OUTPUT_TEST_CAPTION_TOP + (OUTPUT_TEST_CAPTION_HEIGHT + OUTPUT_TEST_CAPTION_INTER) * 3,
        OUTPUT_TEST_CAPTION_WIDTH,
        OUTPUT_TEST_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    int j;
    const char *scc[4] = {
        MW_ROCK_RUN_LEFT,
        MW_ROCK_RUN_ZERO,
        MW_ROCK_RUN_RGHT,
       "0",
    };
    for(j = 0; j < 4; ++j)
    {
        CreateWindow (MH_EDIT_NAME,
            scc[j],
            WS_CHILD | WS_VISIBLE,
            IDC_EDIT1 + j,
            OUTPUT_TEST_CAPTION_LEFT + OUTPUT_TEST_CAPTION_WIDTH,
            OUTPUT_TEST_CAPTION_TOP + (OUTPUT_TEST_CAPTION_HEIGHT + OUTPUT_TEST_CAPTION_INTER) * j,
            OUTPUT_TEST_CAPTION_WIDTH,
            OUTPUT_TEST_CAPTION_HEIGHT,
            hWnd, 0);
    }
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_YARN_CHANGE_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
}
/*
 *
 */
static void OnSetKnitValue(HWND hWnd, int nID)
{
    const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
    ushort n = atoi(sc);
    SetRockTestNeedle(g_knit_data, n);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case IDC_EDIT1:
        {
            ushort needle = -GetRockTestNeedle(g_knit_data);
            RunRockNeedle(g_knit_data, needle);
        }
        break;
    case IDC_EDIT1 + 1:
        {
            ResetRock(g_knit_data, 0);
        }
        break;
    case IDC_EDIT1 + 2:
        {
            ushort needle = GetRockTestNeedle(g_knit_data);
            RunRockNeedle(g_knit_data, needle);
        }
        break;
    case IDC_EDIT1 + 3:
        {
            HWND hTmp = GetDlgItem(hWnd, nID);
            RECT rect;
            GetClientRect(hTmp, &rect);
            ClientToScreen(hTmp, &rect.left, &rect.top);

            ClientToScreen(hTmp, &rect.right, &rect.bottom);
            int value = atoi(GetWindowCaption(hTmp));
            CreateKeyboardWindow(hWnd, rect, 0, 1000, value);
        }
        break;
    case ID_ESC:
        {
            short needle = GetRockPosNeedle(g_knit_data);
            RunRock(g_knit_data, needle);
            PostMessage (hWnd, MSG_CLOSE, 0, 0);
        }
        break;
      }
}

/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_ENTER)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        OnBtnCmd(hWnd, nID);
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 3)
        {
            ++nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID > IDC_EDIT1 && nID <= IDC_EDIT1 + 3)
        {
            --nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
}
/*
 *
 */
static int RockTestWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateRockTestInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 30;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 30;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup (hWnd);
        }
        return 0;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_COMMAND:
        OnBtnCmd(hWnd, LOWORD(wParam));
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls (hWnd);
        }
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_YARN_CHANGE]);
        }
        return 0;
    case MSG_SETDATA:
        OnSetKnitValue(hWnd, LOWORD(wParam));
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateRockTestWindow(HWND hHosting)
{
    HWND hWnd;
    MAINWINCREATE CreateInfo;

    int x = (CX_SCREEN - CX_YARN_CHANGE_BMP) / 2;
    int y = (CY_SCREEN - CY_YARN_CHANGE_BMP) / 2;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_OUTPUT_TEST_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = RockTestWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_YARN_CHANGE_BMP;
    CreateInfo.by = y + CY_YARN_CHANGE_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    short needle = 0;
    GetCurrentNeedle(g_knit_data, &needle);
    SetRockPosNeedle(g_knit_data, needle);
    hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SwitchToKnitState(g_knit_data, FALSE);
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
